import {
  ArgumentMetadata,
  Injectable,
  PipeTransform,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
@Injectable()
export class PipiPipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    // console.log(value, 'metadata', metadata);
    const Instance = plainToInstance(metadata.metatype, value);
    const error = await validate(Instance);
    if (error.length > 0) {
      throw new HttpException(error, HttpStatus.BAD_REQUEST);
    }
    console.log(error);
    return value;
  }
}
